﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraBars;
namespace BSC_OTC_Management
{
    public partial class FrmMainReport : Form
    {
        public FrmMainReport()
        {
            InitializeComponent();
        }

        public FrmMainReport(string _function_name, string _formTitle)
        {
            InitializeComponent();
            lb_caption.Text = "Danh sách báo cáo "+_function_name;
            this._formTitle = _formTitle;
            this._function_name = _function_name;
            //set title for form
            this.Text = _formTitle;
        }

        #region khai báo biến
        string _formName = "BSC_TBL_RPTMASTER";
        Code.CL_Ultility cl_u = new Code.CL_Ultility();
        Code.StaticVariable _code = new Code.StaticVariable();
        DataTable _tableView = new DataTable("SEARCHFLD");
        DataTable v_tbl_ReportList;
        string _function_name="";
        string _formTitle = "";
        #endregion

        private void FrmMainReport_Load(object sender, EventArgs e)
        {  
            _tableView = _code._services.GetViewBy_SearchCode(_formName);
            Loaddata();

        }
        private void Loaddata()
        {
            string cmd_SQL = "";
            try
            {
                v_tbl_ReportList = cl_u._services.GetListReports(_function_name);
                Showdata(v_tbl_ReportList);
                
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.ToString(), " EXECUTE_CMD_ORACLE : " + cmd_SQL);
                _code._services.UPDATE_LOG_FILE("ERR", Code.StaticVariable.userID, cmd_SQL, "EXECUTE_CMD_ORACLE()", ex.ToString());

            }
        }

        private void Showdata(DataTable view)
        {
            // Thay tên dựa theo FieldCode, FieldName
            gridControl_DanhsachBaoCao.DataSource = view;
            gridView3.BestFitColumns();
            //datnt: dont edit any cell in gridView
            gridView3.OptionsBehavior.Editable = false;

            //datnt
            gridView3.MouseDown += new MouseEventHandler(gridView3_MouseDown);

            foreach (DevExpress.XtraGrid.Columns.GridColumn column in this.gridView3.Columns)
            {
                foreach (DataRow row in _tableView.Rows)
                {
                    if (row["fldname"].ToString().ToUpper() == column.ToString())
                    {
                        gridView3.Columns[column.ToString()].Caption = row["caption"].ToString(); ;
                    }
                }
            }
            
        }

        object value = "null";
        void gridView3_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                GridHitInfo hi = gridView3.CalcHitInfo(e.Location);

                if (hi.InRowCell)
                {
                    value = gridView3.GetRowCellValue(hi.RowHandle, "RPTID");

                }
            }
        }


        private void btn_Tao_bao_cao_Click(object sender, EventArgs e)
        {
            try
            {
                if (value.ToString() != "" && value.ToString() != "null")
                {
                    Forms.FrmCreateReport frm_CrReport = new Forms.FrmCreateReport();
                    frm_CrReport.GET_RPTID = value.ToString();
                    frm_CrReport.ShowDialog();
                }
                else
                {
                    MessageBox.Show("Bạn chưa chọn báo cáo", "Thông báo");
                }
            }
            catch (Exception ex)
            {
                _code._services.UPDATE_LOG_FILE("ERR", Code.StaticVariable.userID, "Forms.FrmCreateReport", "btn_Tao_bao_cao_Click()", ex.ToString());

            }

        }

        private void btn_Lam_moi_lenh_chi_thi_Click(object sender, EventArgs e)
        {
            Loaddata();
        }
    }
}
